<template>
  <div>
    <div>
      <nut-input placeholder="请输入文本" v-model="state.text" clearable clearSize="14" />

      <nut-input
        placeholder="请输入密码"
        v-model="state.password"
        type="password"
        :error-message="state.error2"
      />
    </div>
    <div style="padding: 0 20px">
      <nut-button block type="primary" @click="validatePassword">登陆</nut-button>
    </div>
  </div>
</template>
<script lang="ts">
import { reactive } from "vue";
import Taro from '@tarojs/taro';

export default {
  setup() {
    const state = reactive({
      text: "",
      password: "",
      error2: "",
    });

    const validatePassword = async () => {
  try {
    const response = await new Promise((resolve, reject) => {
      Taro.request({
        url: 'https://sunyukq.transpheretms.com:8078/api/Login',
        method: 'POST',
        data: {
          loginname: state.text,
          Password: state.password
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          resolve(res.data);
        },
        fail: function (error) {
          reject(error);
        }
      });
    });

    console.log("响应数据:", response.data);  // 输出响应信息

    // 现在你可以在此处处理 response
    if (response && response.message === "登录成功") {
      state.error2 = "";
      console.log("登录成功");
      Taro.setStorageSync('userData', response.data);
      Taro.navigateTo({
        url:"/pages/attendance/index"
      })
    } else {
      state.error2 = "账号或密码错误";
      console.log("账号或密码错误");
    }

  } catch (error) {
    console.error("登录请求失败:", error);
    state.error2 = "登录请求失败，请稍后再试";
  }
};

    return {
      state,
      validatePassword,
    };
  },
};
</script>